<title>管理员</title>
<div class="layui-fluid">

	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
		<legend>未安装</legend>
	</fieldset>

	<div class="layui-bg-gray" style="padding: 30px;">
		<div class="layui-row layui-col-space15 uninstall">
			
		</div>
	</div>

	<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
		<legend>已安装</legend>
	</fieldset>
	
	<div class="layui-bg-gray" style="padding: 30px;">
		<div class="layui-row layui-col-space15 install">
			
		</div>
	</div>

</div>
<script>
	layui.use(['form', 'table'], function() {
		var $ = layui.jquery,
			form = layui.form,
			table = layui.table;
			let _index = layer.load();
			
		
		get_addon_list();
		function get_addon_list() {
			layui.admin.req({
				url: layui.setter.API_HOST + 'admin/system/addon',
				done: function(res) {
					layer.close(_index);
					if (res.code == 0) {
						// 未安装
						let _uninstall_html = '';
						$.each(res.data.uninstall, function(i, d) {
							_uninstall_html += '<div class="layui-col-md3"> \n\
								<div class="layui-panel"> \n\
									<div class="addon-name">'+ d.title +'</div> \n\
									<div class="addon-desc">'+ d.description +'</div>  \n\
									<div style="padding: 10px 30px; font-size: 12px; text-align: right;"> \n\
										<button type="button" class="layui-btn layui-btn-xs layui-btn-primary addon-btn" data-name="'+ d.name +'" data-tag="install">安装</button> \n\
									</div> \n\
								</div> \n\
							</div>';
						});
						$('.uninstall').html(_uninstall_html);
						
						// 已安装
						let _install_html = '';
						$.each(res.data.install, function(i, d) {
							_install_html += '<div class="layui-col-md3"> \n\
								<div class="layui-panel"> \n\
									<div class="addon-name">'+ d.title +'</div> \n\
									<div class="addon-desc">'+ d.description +'</div>  \n\
									<div style="padding: 10px 30px; font-size: 12px; text-align: right;"> \n\
										<button type="button" class="layui-btn layui-btn-xs layui-btn-primary addon-btn" data-name="'+ d.name +'" data-tag="uninstall">卸载</button> \n\
									</div> \n\
								</div> \n\
							</div>';
						});
						$('.install').html(_install_html);
					}
				}
			});
		}
		
		$(document).on("click", ".addon-btn", function() {
			let _addon_name = $(this).data('name');
			let _tag = $(this).data('tag');
			
			layer.confirm('确定' + (_tag == 'install' ? '安装' : '卸载') + '插件？', function() {
				layui.admin.req({
					url: layui.setter.API_HOST + 'admin/system/addon',
					type: 'post',
					data: {
						'addon_name': _addon_name,
						'tag': _tag
					},
					done: function(res) {
						if (res.code == 0) {
							get_addon_list()
						} else {
							layer.msg('操作失败', {item: 5});
						}
					}
				});
			});
		});
	});
</script>
